package gui;

import gui.configuration.ConfigPanel;
import gui.diseaseEditor.DiseaseEditorPanel;
import gui.environmentEditor.EnvironmentEditorPanel;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Properties;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import language.LanguageProfiler;

/**
 * @author Kevin Wagner
 * @version 1.0
 */
public class WuselWindow extends JFrame implements ActionListener {

	/**
	 * 
	 */
	private static final long serialVersionUID = 3907385083347118786L;

	private Properties props;
	private LanguageProfiler lp;
	private JPanel contentPane;
	private JMenuBar menuBar = new JMenuBar();
	private JMenu fileMenu, editorMenu;
	private JMenuItem homeMenuItem, saveMenuItem, exitMenuItem,
			diseaseMenuItem, envMenuItem, configMenuItem;

	/**
	 * Create the frame.
	 * 
	 * @param props
	 * @param lp
	 */
	public WuselWindow(Properties props, LanguageProfiler lp) {
		super("WUSEL");
		this.props = props;
		this.lp = lp;

		ImageIcon imageIcon = new ImageIcon("LOGO.gif");
		this.setIconImage(imageIcon.getImage());

		setResizable(false);
		setMaximumSize(new Dimension(800, 600));
		setMinimumSize(new Dimension(800, 600));

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);

		setJMenuBar(menuBar);

		fileMenu = new JMenu(lp.getLocalizedString("file"));
		menuBar.add(fileMenu);

		homeMenuItem = new JMenuItem(lp.getLocalizedString("home"));
		homeMenuItem.addActionListener(this);
		fileMenu.add(homeMenuItem);

		saveMenuItem = new JMenuItem(lp.getLocalizedString("save"));
		saveMenuItem.addActionListener(this);
		fileMenu.add(saveMenuItem);

		exitMenuItem = new JMenuItem(lp.getLocalizedString("exit"));
		exitMenuItem.addActionListener(this);
		fileMenu.add(exitMenuItem);

		editorMenu = new JMenu(lp.getLocalizedString("editors"));
		menuBar.add(editorMenu);

		diseaseMenuItem = new JMenuItem(
				lp.getLocalizedString("DiseaseEditorTitle"));
		diseaseMenuItem.addActionListener(this);
		editorMenu.add(diseaseMenuItem);

		envMenuItem = new JMenuItem(
				lp.getLocalizedString("EnvironmentEditorTitle"));
		envMenuItem.addActionListener(this);
		editorMenu.add(envMenuItem);

		configMenuItem = new JMenuItem(lp.getLocalizedString("config"));
		configMenuItem.addActionListener(this);
		menuBar.add(configMenuItem);

		contentPane = new WuselPanel(100);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		setContentPane(contentPane);

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		Object source = e.getSource();
		if (source == homeMenuItem) {
			try {
				switchPanel();
				contentPane = new WuselPanel(100);
				this.setContentPane(contentPane);
				this.validate();
				this.repaint();
			} catch (Exception ex) {

			}
		} else if (source == saveMenuItem) {
			if (((WuselPanelInterface) contentPane).saveData()) {
				JOptionPane.showMessageDialog(this,
						lp.getLocalizedString("saveData"),
						lp.getLocalizedString("saveDataTitle"),
						JOptionPane.INFORMATION_MESSAGE);
			} else {
				JOptionPane.showMessageDialog(this,
						lp.getLocalizedString("saveDataError"),
						lp.getLocalizedString("saveDataErrorTitle"),
						JOptionPane.ERROR_MESSAGE);
			}
		} else if (source == exitMenuItem) {
			WuselWindow.this.dispose();
		} else if (source == diseaseMenuItem) {
			try {
				switchPanel();
				contentPane = new DiseaseEditorPanel(props, lp);
				this.setContentPane(contentPane);
				this.validate();
				this.repaint();
			} catch (Exception ex) {

			}
		} else if (source == envMenuItem) {
			try {
				switchPanel();
				contentPane = new EnvironmentEditorPanel(props, lp);
				this.setContentPane(contentPane);
				this.validate();
				this.repaint();
			} catch (Exception ex) {
				ex.printStackTrace();
			}
		} else if (source == configMenuItem) {
			try {
				switchPanel();
				contentPane = new ConfigPanel(props, lp);
				this.setContentPane(contentPane);
				this.validate();
				this.repaint();
			} catch (Exception ex) {

			}
		}

	}

	private void switchPanel() throws Exception {
		if (((WuselPanelInterface) contentPane).checkForChanges()) {
			if (!((WuselPanelInterface) contentPane).confirmDialog()) {
				throw new Exception();
			}
		}
	}

}
